export class SimpleCache<K, V> {
  private data = new Map<K, V>()
  private fn: (k: K) => V

  constructor(fn: (k: K) => V) {
    this.fn = fn
  }

  public get(k: K): V {
    let v = this.data.get(k)
    if (v === undefined) {
      v = this.fn(k)
      this.data.set(k, v)
    }
    return v
  }
}

export function simpleCache<K, V>(fn: (k: K) => V): SimpleCache<K, V> {
  return new SimpleCache(fn)
}
